package com.htudata.zabbix.event.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.htudata.core.util.operation.ArrayOperationUtils;
import com.htudata.core.util.operation.StringOperationUtils;
import com.htudata.zabbix.api.service.EventZabbixApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EventService {

  @Autowired
  private EventZabbixApiService eventZabbixApiService;
  
  public static boolean isValidEventId(String id) {
    if (StringOperationUtils.isPositiveInt(id))
      return true; 
    return false;
  }
  
  public JSONArray updateEvents(String[] eventIdArr, int action, String message, int severity) throws  Exception{
    JSONObject params = new JSONObject();
    if (ArrayOperationUtils.isValidArr((Object[])eventIdArr))
      params.put("eventids", eventIdArr); 
    params.put("action", Integer.valueOf(action));
    params.put("message", message);
    params.put("severity", Integer.valueOf(severity));
    String json = this.eventZabbixApiService.updateEvents(params);
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return null; 
    JSONArray result = root.getJSONArray("result");
    return result;
  }
}
